package hva.team2.viavia;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.LinearLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.jsambells.directions.RouteAbstract;
import com.jsambells.directions.ParserAbstract.Mode;
import com.jsambells.directions.RouteAbstract.RoutePathSmoothness;
import com.jsambells.directions.google.DirectionsAPI;
import com.jsambells.directions.google.DirectionsAPIRoute;

public class MapRountingTest extends FragmentActivity implements com.jsambells.directions.ParserAbstract.IDirectionsListener {

	private GoogleMap map;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_map_test);
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getMap();
        
        if(map == null){
        	Log.e("MAP","map fails");
        }

		// Find a route
		List<GeoPoint> waypoints = new ArrayList<GeoPoint>();

    	// Lets go on a tower tour!
		waypoints.add(new GeoPoint(43642085,-79386976)); // CN Tower
		waypoints.add(new GeoPoint(44588328,-104698527)); // Devils Tower
		waypoints.add(new GeoPoint(37802341,-122405811)); // Coit Tower

		DirectionsAPI directions = new DirectionsAPI();
		directions.getDirectionsThruWaypoints(
			waypoints, 
			DirectionsAPI.Mode.DRIVING, 
			this
		);

        
    }

	/* MapActivity */

  
	/* IDirectionsListener */
    
	public void onDirectionsAvailable(RouteAbstract route, Mode mode) {
		// TODO Auto-generated method stub
		// Add it to a map

		// Add a directions overlay to the map.
		// This is just a quick example to draw the line on the map.
		//DirectionsOverlay directions = new DirectionsOverlay();
		//directions.setRoute((DirectionsAPIRoute)route);
		//map.getOverlays().add(directions);
		//map.requestLayout();
		
		DirectionsAPIRoute droute = (DirectionsAPIRoute)route;
		
		List<GeoPoint> gpl = droute.getGeoPointPath(RoutePathSmoothness.FINE);
		
		PolylineOptions plo = new PolylineOptions();
		for(GeoPoint gp : gpl){
			
			LatLng coords = new LatLng(gp.getLatitudeE6()/1E6, gp.getLongitudeE6()/1E6);
			plo.add(coords);
		}
		plo.width(5).color(Color.RED);
		map.addPolyline(plo);
		//Log.e("ROUTE", "WAYPOINTS SIZE " +droute.getGeoPointPath(RoutePathSmoothness.FINE).size());
		

	}
	public void onDirectionsNotAvailable() {
		// TODO Auto-generated method stub
		// Show an error?
	}

/*	public class DirectionsOverlay extends Overlay {

		static final String TAG = "DirectionsOverlay";

		// The route to draw
		private DirectionsAPIRoute mRoute;

		// Our Paint
		Paint pathPaint = new Paint();

		public DirectionsOverlay() {
			this.pathPaint.setAntiAlias(true);
		}

		public DirectionsOverlay setRoute( DirectionsAPIRoute route) {
			this.mRoute = route;
			return this;
		}

		// This function does some fancy drawing
		public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {

			// This method will be called twice. Once in the 
			// shadow phase, skewed and darkened, then again in 
			// the non-shadow phase. 

		}

	}*/

}